package com.llkj.qingnianguoxue.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.base.MyApplication;
import com.llkj.qingnianguoxue.bean.ZhiBoDetailBean;
import com.llkj.qingnianguoxue.util.ImageLoadingUtil;
import com.llkj.qingnianguoxue.util.StringUtils;

import java.util.List;

/**
 * 直播聊天
 */
public class ZhiBoChatAdapter extends BaseQuickAdapter<ZhiBoDetailBean.MessageBean, BaseViewHolder> {
    private String mUserId;
    public ZhiBoChatAdapter(int layoutResId, List<ZhiBoDetailBean.MessageBean> data) {
        super(layoutResId, data);
        mUserId = MyApplication.getInstance().sputil.getString(ConstantUserInfo.USER_ID);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, ZhiBoDetailBean.MessageBean item) {
//        setView(baseViewHolder, item);
        ImageView ivHead = baseViewHolder.getView(R.id.iv_head);
        ImageLoadingUtil.loadingImg(ivHead, item.getUserpic(), 0);
        baseViewHolder.setText(R.id.tv_name,item.getUsername());
        baseViewHolder.setText(R.id.tv_time,item.getAddtime());
        baseViewHolder.setText(R.id.tv_description,item.getContent());
    }

    private void setView(BaseViewHolder baseViewHolder, ZhiBoDetailBean.MessageBean item) {
        FrameLayout rootView = baseViewHolder.getView(R.id.fl_chat_add);

        rootView.removeAllViews();
        View itemView = null;
        ImageView ivHead = null;
        TextView tvName = null, tvTime = null, tvContent = null;
        if (!StringUtils.isEmpty(mUserId) && mUserId.equals(item.getUid())) {
            itemView = LayoutInflater.from(mContext).inflate(R.layout.item_zhibo_chat_right, null);
        } else {
            itemView = LayoutInflater.from(mContext).inflate(R.layout.item_zhibo_chat_left, null);
        }
        rootView.addView(itemView);
        ivHead = (ImageView) itemView.findViewById(R.id.iv_head_left);
        tvName = (TextView) itemView.findViewById(R.id.tv_name);
        tvTime = (TextView) itemView.findViewById(R.id.tv_time);
        tvContent = (TextView) itemView.findViewById(R.id.tv_chat_content);

        ImageLoadingUtil.loadingImg(ivHead, item.getUserpic(), 0);
        tvName.setText(item.getUsername());
        tvTime.setText(item.getAddtime());
        tvContent.setText(item.getContent());

    }
}
